<?php /* 	These code was generated using phpCIGen v 0.1.a (21/04/2009)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com,
    #songbee	mukhlisona@gmail.com
	#CV. Trust Solution, jl. Saronojiwo 19 Surabaya, http://www.ts.co.id

	+ Module  		: m_product Model
	+ Description	: For record model process back-end
	+ Filename 		: c_m_product.php
 	+ creator 		:
 	+ Created on 10/May/2011 17:26:04

*/

class M_public_function extends CI_Model{

	//constructor
	function M_public_function() {
		parent::__construct();
	}

	
	//function for get list record

	function get_city($filter,$start,$end){
		$filter = strtolower($filter);
		$sql = "SELECT city_id,
				city_name
			FROM m_city";

		if($filter<>""){
			$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
			$sql .= " (lower(city_name) LIKE '%".addslashes($filter)."%' )";
		}

		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
		$limit = $sql." OFFSET ".$start." LIMIT ".$end;
		$result = $this->db->query($limit);

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_m_staff($query,$start,$end){
		$sql = "SELECT staff_id,
				staff_name
			FROM m_staff";

		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
		//$limit = $sql." OFFSET ".$start." LIMIT ".$end;
                $limit = $sql." LIMIT ".$start.",".$end;
		$result = $this->db->query($limit);

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	
	function get_usergroup($filter,$start,$end){
		$sql = "SELECT usergroup_id,
				usergroup_name
			FROM m_usergroup";

		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
		$limit = $sql." OFFSET ".$start." LIMIT ".$end;
		$result = $this->db->query($limit);

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
	
	function get_m_customer($query,$start,$end){
		$sql = "SELECT customer_id,
				customer_name
			FROM m_customer";

		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
		//$limit = $sql." OFFSET ".$start." LIMIT ".$end;
                $limit = $sql." LIMIT ".$start.",".$end;
		$result = $this->db->query($limit);

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
        
        function get_t_order_forbill($filter,$start,$end){
		$filter = strtolower($filter);
                $sql = "SELECT
                                order_id,
                                order_code,
                                customer_name
                        FROM
                                t_order
                        LEFT JOIN m_customer ON m_customer.customer_id = t_order.m_customer_id
                        WHERE
                                order_stat IN('a', 'b')";
                if($filter<>""){
			$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
			$sql .= " (lower(order_code) LIKE '%".addslashes($filter)."%' )";
		}
                
		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
                $limit = $sql." LIMIT ".$start.",".$end;
		$result = $this->db->query($limit);

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
        
        function get_t_order($filter,$start,$end){
		$filter = strtolower($filter);
                $sql = "SELECT order_id, order_code
                        FROM t_order";
                if($filter<>""){
			$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
			$sql .= " (lower(order_code) LIKE '%".addslashes($filter)."%' )";
		}
                
		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
                $limit = $sql." LIMIT ".$start.",".$end;
		$result = $this->db->query($limit);

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}    
        
        function get_totaltag_byorder($filter){
		//$filter = strtolower($filter);
                $sql = "SELECT SUM(orderdet_qty*orderdet_price) as totale
                        FROM t_order
                        LEFT JOIN t_orderdet on t_orderdet.t_order_id = t_order.order_id
                        WHERE t_order.order_id = '".$filter."'";
                
		$rssql = $this->db->query($sql);
		$nbrows = $rssql->num_rows();
                //$this->firephp->log("hasil : ".$result);
                if($nbrows>0)
                    {
                        $record = $rssql->row();
                        return $record->totale;
                        //return 1000;
                    } 
                else 
                    {
                        return -1;
                    }
	}          
        
        function get_t_bill_forpayment($filter,$start,$end){
                $filter = strtolower($filter);
                $sql = "SELECT bill_id, bill_no
                        FROM t_bill
                        WHERE bill_stat IN ('a')";
                if($filter<>""){
			$sql .=eregi("WHERE",$sql)? " AND ":" WHERE ";
			$sql .= " (lower(order_code) LIKE '%".addslashes($filter)."%' )";
		}
                
		$result = $this->db->query($sql);
		$nbrows = $result->num_rows();
		//$limit = $sql." OFFSET ".$start." LIMIT ".$end;
                $limit = $sql." LIMIT ".$start.",".$end;
		$result = $this->db->query($limit);

		if($nbrows>0){
			foreach($result->result() as $row){
				$arr[] = $row;
			}
			$jsonresult = json_encode($arr);
			return '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			return '({"total":"0", "results":""})';
		}
	}
}
?>